// object encapsulating an hour-minute pair

module.exports = (function() {
  "use strict";

  // constructor
  function HourMinute(hour, minute) {
    function parseIntWithLeading0(thing) {
      if (typeof(thing) === "number")
        return thing;
      var match = thing.toString().match(/^0*(\d+)$/);
      return parseInt(match[1]);
    }

    this.hour = parseIntWithLeading0(hour);
    this.minute = parseIntWithLeading0(minute.toString());
  }

  HourMinute.prototype.toString = function () {
    function formatNN(n) {
      if (n>=10) return n.toString();
      return "0" + n;
    }
    return formatNN(this.hour) + formatNN(this.minute);
  }

  HourMinute.prototype.equals = function (that) {
    if (this === that) return true;
    if (!(that instanceof HourMinute)) return false;
    return this.hour === that.hour &&
      this.minute === that.minute;
  }

  HourMinute.prototype.smallerNotEqual = function (that) {
    if (!(that instanceof HourMinute))
      throw "bug!";
    if (this.hour < that.hour) return true;
    if (this.hour > that.hour) return false;
    return this.minute < that.minute;
  }

  HourMinute.regexp = /(\d{1,2}):?(\d\d)/;
  HourMinute.parse = function(text) {
    var match = text.match(HourMinute.regexp);
    if (!match)
      throw "cannot parse hours: " + text;
    return new HourMinute(match[1], match[2]);
  }

  HourMinute.REsrc = /\d{1,2}:?\d\d/.source;


  return HourMinute;
}());

